package com.front;

import com.admin.service.HospitalizationService;
import com.admin.entity.Hospitalization;
import com.admin.sys.utils.jwt.JwtUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import com.admin.sys.utils.admin.StringUtils;
import javax.servlet.http.HttpServletRequest;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
*【住院信息】管理
*/
@RestController
@RequestMapping("/api/hospitalization")
public class ApiHospitalizationController extends BaseController {

    @Autowired
    private HospitalizationService hospitalizationService;

    /**
     *【住院信息】获取分页数据
     */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<Hospitalization> page, Hospitalization hospitalization) {
        hospitalization.setRegisterId(JwtUtils.getCurrentUserId());
        IPage<Hospitalization> iPage = hospitalizationService.getPage(page, hospitalization);
        return ResultInfo.ok("获取分页成功",iPage);
    }

    /**
    * 【住院信息】获取列表数据
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Hospitalization hospitalization) {
        List<Hospitalization> list = hospitalizationService.getList(hospitalization);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    *【住院信息】根据对象数据
    */
    @RequestMapping("/get")
    public ResultInfo get(Hospitalization hospitalization) {
        Hospitalization entity = hospitalizationService.get(hospitalization);
        return ResultInfo.ok("获取成功",entity);
    }

    /**
     *【住院信息】保存或修改
     */
    @RequestMapping("/sub")
    public ResultInfo saveOrUpdate(HttpServletRequest request,Hospitalization hospitalization){
        try {
            if(StringUtils.isEmpty(hospitalization.getId())){
                hospitalizationService.insert(hospitalization);//新增
                return ResultInfo.ok("保存成功！");
            }else{
                hospitalizationService.update(hospitalization);//修改
                return ResultInfo.ok("修改成功！");
            }
        } catch (Exception e) {
            return ResultInfo.error("保存失败！失败信息:"+e.getMessage());
        }
    }

    /**
     *【住院信息】根据id删除
     */
    @RequestMapping("/delete")
    public ResultInfo delete(HttpServletRequest request, Hospitalization hospitalization) {
        hospitalizationService.delete(hospitalization.getId());
        return ResultInfo.ok("删除成功！");
    }

    /**
    * 【住院信息】批量删除
    */
    @RequestMapping("/delAll")
    public ResultInfo delAll(String[] ids) {
        hospitalizationService.delAll(ids);
        return ResultInfo.ok("批量删除成功！");
    }

}



